package com.claude.mainactivity;

import java.util.ArrayList;

import org.json.JSONObject;

import com.android.volley.VolleyError;
import com.claude.bean.Subject;
import com.claude.bean.User;
import com.claude.communication.RequestManager;
import com.claude.communication.RequestNode;
import com.claude.communication.RequestNode.RequestListener;
import com.claude.sql.AccountTableManager;
import com.claude.sql.SQLManager;
import com.claude.sql.SubjectTableManager;
import com.claude.util.Configure;
import com.claude.util.Parser;
import com.claude.util.QueryParser;
import com.special.ResideMenuDemo.MenuActivity;
import com.special.ResideMenuDemo.R;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Window;
import android.widget.Toast;

public class InitAppActivity extends Activity {
	final private int LOGIN_REQUEST = 1;
	private MyApplication mMyApplication = null;
	private Configure configure = null;
	private RequestManager mRequestManager = null;
	private RequestListener mRequestListener = null;
	private AccountTableManager mAccountTableManager = null;
	private SQLiteDatabase mSQLiteDatabase = null;
	private SubjectTableManager mSubjectTableManager = null;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		this.setContentView(R.layout.activity_init);

		mMyApplication = (MyApplication) getApplication();
		configure = Configure.getInstance();
		mRequestManager = RequestManager.newInstance();
		if (mMyApplication.getOfflineState()) {
			this.getResource();
		} else {
			this.initResource();
		}
	}

	public void onResume() {
		mSQLiteDatabase = new SQLManager(this).getWritableDatabase();
		mAccountTableManager = new AccountTableManager(mSQLiteDatabase);
		super.onResume();
	}

	public void onStop() {
		if (mRequestManager != null) {
			mRequestManager.stopRequest();
		}
		super.onStop();
		this.finish();
	}

	// Location data resource
	private void initResource() {
		// init database object
		if (mSQLiteDatabase == null) {
			mSQLiteDatabase = new SQLManager(this).getReadableDatabase();
			if (mAccountTableManager == null) {
				mAccountTableManager = new AccountTableManager(mSQLiteDatabase);
				mSubjectTableManager = new SubjectTableManager(mSQLiteDatabase);
			}
		}
		User user = mMyApplication.getUser();
		Cursor cursorUser = mAccountTableManager.search(user.getAccount(),
				user.getPassword());
		user = QueryParser.getUser(cursorUser);
		if (user != null) {
			mMyApplication.setUser(user);
			// init subject
			Cursor cursorSub = mSubjectTableManager.search(user.getTeacherNumber());
			ArrayList<Subject> subjects = QueryParser.getSubjects(cursorSub);
			mMyApplication.setSubjects(subjects);		
			gotoMain();
		}else{
			Toast.makeText(InitAppActivity.this, "用户名密码错误", 1000).show();
			returnToLogin();
		}
	}

	private void getResource() {
		mRequestListener = new MyRequestListener();
		RequestNode requestNode = new RequestNode(getParamsWithLogin(),
				mRequestListener, null, LOGIN_REQUEST);
		mRequestManager.addRequest(requestNode);
		mRequestManager.startRequest();
	}

	private String getParamsWithLogin() {
		User user = mMyApplication.getUser();
		return String.format(configure.getLoginUrl() + "?id=%s&password=%s",
				user.getAccount(), user.getPassword());
	}

	class MyRequestListener implements RequestListener {
		public void onSuccess(JSONObject jsonObject, int tag) {
			if (tag == LOGIN_REQUEST) {
				User user = Parser.getUser(jsonObject);
				user.setAccount(mMyApplication.getUser().getAccount());
				user.setPassword(mMyApplication.getUser().getPassword());
				mMyApplication.setUser(user);
				mAccountTableManager.insert(user.getTeacherNumber(),
						user.getAccount(), user.getPassword());
				ArrayList<Subject> subjects = Parser.getSubjects(jsonObject);
				mMyApplication.setSubjects(subjects);
				mSQLiteDatabase.close();
				gotoMain();
			}
		}

		public void onError(VolleyError volleyError, int tag) {
			if (tag == LOGIN_REQUEST) {
				if (mMyApplication.getSubjects() == null) {
					Toast.makeText(InitAppActivity.this, "用户名密码错误", 1000).show();
					returnToLogin();
				}
			}
		}
	}

	private void returnToLogin() {
		Intent intent = new Intent();
		intent.setClass(InitAppActivity.this, LoginActivity.class);
		InitAppActivity.this.startActivity(intent);
	}

	private void gotoMain() {
		Intent intent = new Intent();
		intent.setClass(InitAppActivity.this, MenuActivity.class);
		InitAppActivity.this.startActivity(intent);
	}
}
